LArk.widget.bindClass(LArk.widget.BaseWidget.extend({
    options: {
        view: {
            type: "window",
            url: "view.html",
            windowOptions: {
                width: 250
            }
        }
    },
    drawControl: null,
    create: function() {
        var n = this;
        this.drawControl = new LArk.Draw(this.viewer, {
            hasEdit: !1,
            nameTooltip: !0
        }), this.drawControl.on(LArk.draw.event.DrawCreated, function(t) {
            var i = t.entity;
            n.startEditing(i)
        }), this.drawControl.on(LArk.draw.event.EditStart, function(t) {
            var i = t.entity;
            n.startEditing(i)
        }), this.drawControl.on(LArk.draw.event.EditMovePoint, function(t) {
            var i = t.entity;
            n.startEditing(i)
        }), this.drawControl.on(LArk.draw.event.EditRemovePoint, function(t) {
            var i = t.entity;
            n.startEditing(i)
        }), 
        this.drawControl.on(LArk.draw.event.EditStop, function(t) {
            var i = t.entity;
            console.log(i)
            n.stopEditing(i), n.saveEntity(i)
        }), 
        // 绑定到图层控制面板
        bindToLayerControl({
            pid: 0,
            name: "标绘",
            visible: !0,
            onAdd: function() {
                n.drawControl.setVisible(!0)
            },
            onRemove: function() {
                n.drawControl.setVisible(!1)
            },
            onCenterAt: function(t) {
                var i = n.drawControl.getEntitys();
                n.viewer.flyTo(i, {
                    duration: t
                })
            }
        })
        // this.getList()
    },
    viewWindow: null,
    winCreateOK: function(t, i) {
        this.viewWindow = i
    },
    activate: function() {
        this.drawControl.hasEdit(!0)
    },
    disable: function() {
        this.viewWindow = null, this.drawControl.stopDraw(), this.drawControl.hasEdit(!1)
    },
    startDraw: function(t) {
        console.log(JSON.stringify(t)), this.drawControl.startDraw(t)
    },
    startEditingById: function(t) {
        var i = this.drawControl.getEntityById(t);
        null != i && (this.viewer.flyTo(i), i.startEditing())
    },
    startEditing: function(t) {
        if (null != this.viewWindow) {
            var i = this.drawControl.getCoordinates(t);
            this.viewWindow.plotEdit.startEditing(t.attribute, i)
        }
    },
    stopEditing: function(t) {
        null != this.viewWindow && this.viewWindow.plotEdit.stopEditing()
        console.log(t,'结束绘制');
    },
    updateAttr2map: function(t) {
        this.drawControl.updateAttribute(t)
    },
    updateGeo2map: function(t, i) {
        var n = [];
        if (i) for (var r = 0; r < t.length; r += 3) {
            var e = Cesium.Cartesian3.fromDegrees(t[r], t[r + 1], t[r + 2]);
            n.push(e)
        } else for (r = 0; r < t.length; r += 2) {
            e = Cesium.Cartesian3.fromDegrees(t[r], t[r + 1], 0);
            n.push(e)
        }
        return this.drawControl.setPositions(1 == n.length ? n[0] : n), n.length <= 3 && this.centerCurrentEntity(), n
    },
    centerCurrentEntity: function() {
        var t = this.drawControl.getCurrentEntity();
        if (null != t) {
            var i = this.drawControl.getPositions(t);
            1 < i.length ? this.viewer.camera.flyTo({
                destination: Cesium.Rectangle.fromCartesianArray(i)
            }) : this.viewer.flyTo(t)
        }
    },
    getGeoJson: function() {
        return this.drawControl.toGeoJSON()
    },
    getCurrentEntityGeoJson: function() {
        var t = this.drawControl.getCurrentEntity();
        if (null != t) return this.drawControl.toGeoJSON(t)
    },
    jsonToLayer: function(t, i, n) {
        if (null != t) return this.drawControl.jsonToEntity(t, i, n)
    },
    deleteAll: function() {
        this.drawControl.deleteAll(), haoutil.storage.del(this.storageName)
    },
    deleteEntity: function(t) {
        var i = this.drawControl.getEntityById(t);
        null != i && this.drawControl.deleteEntity(i)
    },
    deleteCurrentEntity: function() {
        var t = this.drawControl.getCurrentEntity();
        this.drawControl.deleteEntity(t);
        console.log(t,'delecurent')
        if (null != t) {
            this.drawControl.deleteEntity(t);
            var i = JSON.stringify(this.getGeoJson());
            haoutil.storage.add(this.storageName, i)
        }
    },
    hasEdit: function(t) {
        this.drawControl.hasEdit(t)
    },
    query: function(t, i) {
        for (var n = this.drawControl.getEntitys(), r = [], e = 0, o = 0; o < n.length; o++) {
            var a, s = n[o];
            if ("label" === s.attribute.type ? a = s.attribute.style.text : s.attribute.attr && (a = s.attribute.attr.name), null != a && -1 != a.indexOf(t) && (r.push({
                name: a,
                type: "标绘 - " + s.attribute.name,
                _datatype: "plot",
                _entity: s
            }), i && i < ++e)) break
        }
        return r
    },
    last_window_param: null,
    showEditAttrWindow: function(t) {
        this.last_window_param = t
    },
    saveWindowEdit: function(t) {
        this.viewWindow.plotEdit.updateAttr(this.last_window_param.parname, this.last_window_param.attrName, t), layer.close(layer.index)
    },
    storageName: "LArkgis_plot",
    getList: function() {
        var i = this,
            t = haoutil.storage.get(this.storageName);
        null == t || "null" == t ? $.getJSON("widgets/plot/config/draw-demo.json", function(t) {
            i.isActivate && i.jsonToLayer(t, !0, !0)
        }) : i.jsonToLayer(JSON.parse(t), !0, !0)
    },
    saveEntity: function(t) {
        if (null != this.viewWindow) {
            t.attribute.attr = t.attribute.attr || {}, t.attribute.attr.id = (new Date).format("yyyyMMddHHmmss");
            var i = JSON.stringify(this.getGeoJson());
            haoutil.storage.add(this.storageName, i)
            console.log(t,'绘制完成后保存实体','saveEntity');
        }
    }
}));